/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trefliq.persistence.entity;

import java.util.Calendar;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.Test;
import static org.junit.Assert.*;
import trefliq.persistence.entity.OrdersTestSuite.*;

/**
 *
 * @author Derek
 */
@RunWith(Suite.class)
@Suite.SuiteClasses({OrderInsertTest.class, OrderUpdateTest.class, OrderDeleteTest.class})
public class OrdersTestSuite {

    private static EntityManagerFactory emf;
    private static EntityManager em;
    private static EntityTransaction tx;
    
    @BeforeClass
    public static void setUpClass() throws Exception {
        emf = Persistence.createEntityManagerFactory("trefliqPU");
        em = emf.createEntityManager();
    }
    
    @AfterClass
    public static void tearDownClass() throws Exception {
        em.close();
        emf.close();
    }

    @Before
    public void initTransaction() {
        tx = em.getTransaction();
    }

    @After
    public void tearDown() throws Exception {
    }
    
    public static class OrderInsertTest {
        public OrderInsertTest() {}
               
        @Test 
        public void testOrdersInsert() {
            Orders orders = new Orders();
            orders.setStatus("New");
            orders.setCustomerId(new Long(100));
            orders.setDateCreated(Calendar.getInstance().getTime());
            tx = em.getTransaction();
            tx.begin();
            em.persist(orders);
            tx.commit();
            assertNotNull("ID should have been generated and populated after persist", 
                    orders.getId()); 
        }
        
    }
    
    public static class OrderUpdateTest {
        public OrderUpdateTest() {}
        
        @Test
        public void testOrdersUpdate() {
            tx = em.getTransaction();
            tx.begin();
            Orders orders = em.find(Orders.class, new Long(1));
            orders.setStatus("Order Sent");
            tx.commit();

            Orders updatedOrders = em.createQuery("select o from Orders o", Orders.class).getSingleResult();
            assertEquals(orders, updatedOrders);
        }
    }
    
    public static class OrderDeleteTest {
        public OrderDeleteTest() {}
        @Test
        public void testOrdersDelete() {
            int priorCount = em.createQuery("select o from Orders o").getResultList().size();
            tx = em.getTransaction();
            tx.begin();
            Orders orders = em.find(Orders.class, new Long(1));
            em.remove(orders);
            tx.commit();

            int afterCount = em.createQuery("select o from Orders o").getResultList().size();

            assertEquals(afterCount + 1, priorCount);
        }        
    }
}
